package com.why.handler;

import com.why.enums.ErrorEnum;
import com.why.exception.RpcException;
import com.why.message.RpcRequest;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Slf4j
public class RequestHandler {





    public static Object handler(Object service, RpcRequest request) {
        Object result=null;
        try {
            log.info("requesthandler开始工作....");
            Method method = service.getClass().getMethod(request.getMethodName(), request.getParameterTypes());
            if (method == null) {
                throw new RpcException(ErrorEnum.NOT_FOUND);
            }

            result = method.invoke(service, request.getMethodParameters());


        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            log.error("RequestHandler在执行对应方法的时候发生了错误");
            e.printStackTrace();
        }
        return result;
    }


}